package com.ting.encrypt.factory;


import com.ting.encrypt.enums.SensitiveType;
import com.ting.encrypt.handler.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: guohaibing
 * @date: 2022/9/29 15:00
 */
public class SensitiveHandlerFactory {

  private static final Map<SensitiveType, SensitiveTypeHandler> HANDLER_REGISTRY =
      new ConcurrentHashMap<>();

  static {
    HANDLER_REGISTRY.put(SensitiveType.NONE, new NoneSensitiveHandler());
    HANDLER_REGISTRY.put(SensitiveType.DEFAUL, new DafaultSensitiveHandler());
    HANDLER_REGISTRY.put(SensitiveType.CHINESE_NAME, new NameSensitiveHandler());
    HANDLER_REGISTRY.put(SensitiveType.ID_CARD, new IDCardSensitiveHandler());
    HANDLER_REGISTRY.put(SensitiveType.MOBILE_PHONE, new MobilePhoneSensitiveHandler());
    HANDLER_REGISTRY.put(SensitiveType.ADDRESS, new AddressSensitiveHandler());
    HANDLER_REGISTRY.put(SensitiveType.EMAIL, new EmailSensitiveHandler());
    HANDLER_REGISTRY.put(SensitiveType.BANK_CARD, new BandCardSensitiveHandler());
    HANDLER_REGISTRY.put(SensitiveType.FIXED_PHONE, new FixedPhoneSensitiveHandler());
  }

  /**
   * 注册额外的脱敏规则 可通过cn.tsign.encrypt.component.enums.SensitiveType中预留的枚举进行拓展
   *
   * @param sensitiveTypeHandler
   */
  public static void registry(SensitiveTypeHandler sensitiveTypeHandler) {
    HANDLER_REGISTRY.put(sensitiveTypeHandler.getSensitiveType(), sensitiveTypeHandler);
  }

  public static SensitiveTypeHandler getSensitiveTypeHandler(SensitiveType sensitiveType) {
    SensitiveTypeHandler sensitiveTypeHandler = HANDLER_REGISTRY.get(sensitiveType);
    if (sensitiveTypeHandler == null) {
      return new NoneSensitiveHandler();
    }
    return sensitiveTypeHandler;
  }
}
